@echo off
chcp 65001 >nul
echo ========================================
echo 修复APK安装问题
echo ========================================

echo.
echo 1. 检查当前构建配置...
echo.

REM 检查是否存在release APK
if not exist "app\release\app-release.apk" (
    echo 错误: 未找到release APK文件
    echo 请先运行构建命令: gradlew assembleRelease
    pause
    exit /b 1
)

echo 找到APK文件: app\release\app-release.apk
echo APK大小: 
dir "app\release\app-release.apk" | find "app-release.apk"

echo.
echo 2. 修复构建配置...

REM 备份原始build.gradle.kts
copy "app\build.gradle.kts" "app\build.gradle.kts.backup"

echo.
echo 3. 更新build.gradle.kts以修复安装问题...

REM 创建修复后的build.gradle.kts
(
echo plugins {
echo     alias^(libs.plugins.android.application^)
echo     alias^(libs.plugins.kotlin.android^)
echo }
echo.
echo android {
echo     namespace = "com.example.myapplication"
echo     compileSdk = 34
echo.
echo     defaultConfig {
echo         applicationId = "com.example.myapplication"
echo         minSdk = 21
echo         targetSdk = 34
echo         versionCode = 1
echo         versionName = "1.0"
echo         testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
echo         multiDexEnabled = true
echo     }
echo.
echo     buildTypes {
echo         release {
echo             isMinifyEnabled = false
echo             isShrinkResources = false
echo             proguardFiles^(
echo                 getDefaultProguardFile^("proguard-android-optimize.txt"^),
echo                 "proguard-rules.pro"
echo             ^)
echo             signingConfig = signingConfigs.debug
echo         }
echo         debug {
echo             isMinifyEnabled = false
echo             isShrinkResources = false
echo         }
echo     }
echo     compileOptions {
echo         sourceCompatibility = JavaVersion.VERSION_11
echo         targetCompatibility = JavaVersion.VERSION_11
echo     }
echo     kotlinOptions {
echo         jvmTarget = "11"
echo     }
echo     testOptions {
echo         unitTests.isIncludeAndroidResources = false
echo         unitTests.all {
echo             it.enabled = false
echo         }
echo     }
echo     packaging {
echo         resources {
echo             excludes += "/META-INF/{AL2.0,LGPL2.1}"
echo         }
echo     }
echo }
echo.
echo dependencies {
echo     implementation^(libs.androidx.core.ktx^)
echo     implementation^(libs.androidx.appcompat^)
echo     implementation^("androidx.multidex:multidex:2.0.1"^)
echo     implementation^("androidx.recyclerview:recyclerview:1.3.2"^)
echo     implementation^("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"^)
echo     implementation^("com.google.android.material:material:1.11.0"^)
echo     implementation^("org.java-websocket:Java-WebSocket:1.5.4"^)
echo     implementation^("com.google.firebase:firebase-messaging:23.4.0"^)
echo     implementation^("com.google.firebase:firebase-analytics:21.5.0"^)
echo     implementation^("com.squareup.okhttp3:okhttp:4.12.0"^)
echo     implementation^("com.squareup.okhttp3:logging-interceptor:4.12.0"^)
echo     implementation^("com.google.code.gson:gson:2.10.1"^)
echo     implementation^("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"^)
echo     implementation^("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0"^)
echo     implementation^("androidx.work:work-runtime-ktx:2.9.0"^)
echo }
echo.
echo tasks.withType^<Test^> {
echo     enabled = false
echo }
) > "app\build.gradle.kts"

echo 4. 清理并重新构建...

REM 清理项目
call gradlew clean

REM 重新构建release版本
call gradlew assembleRelease

echo.
echo 5. 验证新APK...

if exist "app\release\app-release.apk" (
    echo 新APK构建成功！
    echo 文件大小:
    dir "app\release\app-release.apk" | find "app-release.apk"
    
    echo.
    echo 6. 创建安装说明文件...
    
    (
    echo APK安装说明
    echo ================
    echo.
    echo 如果无法安装，请按以下步骤操作：
    echo.
    echo 1. 确保设备已开启"未知来源"应用安装权限
    echo    - 设置 ^> 安全 ^> 未知来源
    echo    - 或设置 ^> 应用 ^> 特殊应用访问权限 ^> 安装未知应用
    echo.
    echo 2. 如果提示"解析包时出现问题"
    echo    - 检查设备Android版本是否支持（最低要求：Android 5.0）
    echo    - 尝试重启设备后重新安装
    echo.
    echo 3. 如果提示"应用未安装"
    echo    - 卸载旧版本应用（如果存在）
    echo    - 清除设备存储空间
    echo.
    echo 4. 如果仍然无法安装
    echo    - 尝试使用ADB安装：adb install app-release.apk
    echo    - 或通过应用商店分发
    echo.
    echo 技术信息：
    echo - 最低Android版本：5.0 (API 21)
    echo - 目标Android版本：14 (API 34)
    echo - 应用包名：com.example.myapplication
    echo - 版本号：1.0
    ) > "APK安装说明.txt"
    
    echo 已创建安装说明文件：APK安装说明.txt
) else (
    echo 错误：APK构建失败
    echo 请检查构建日志
)

echo.
echo 7. 创建兼容性测试脚本...

(
echo @echo off
echo echo 测试APK兼容性...
echo echo.
echo adb devices
echo echo.
echo echo 安装APK到设备...
echo adb install -r "app\release\app-release.apk"
echo echo.
echo echo 检查安装结果...
echo adb shell pm list packages ^| find "com.example.myapplication"
echo echo.
echo pause
) > "test_apk_installation.bat"

echo 已创建测试脚本：test_apk_installation.bat

echo.
echo ========================================
echo 修复完成！
echo ========================================
echo.
echo 新APK位置：app\release\app-release.apk
echo 安装说明：APK安装说明.txt
echo 测试脚本：test_apk_installation.bat
echo.
echo 如果仍有问题，请：
echo 1. 检查设备Android版本是否支持
echo 2. 确保开启了"未知来源"安装权限
echo 3. 尝试卸载旧版本后重新安装
echo.
pause 